
package Servlets;

import Entidades.Tema;
import iBatis.Persistencia;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *  LuxarJSP
 *      - Javier Ruiz Hidalgo
 *      - Miguel Alejandro Esteban Ordoñez
 */

/**
 *  Descripción de la Clase:
 *      - Servlet que trata la creación de Temas
 */

@WebServlet(name="ServletNuevoTema", urlPatterns={"/ServletNuevoTema"})
public class ServletNuevoTema extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        HttpSession session = request.getSession(false);
        if (session != null) {
            if(request.getParameter("crear") != null) {
                try {
                    //Comprueba que el asunto introducido no esta vacío
                    if(request.getParameter("asunto") != null){
                        //Todo OK. Instanciamos un Tema y lo creamos
                        Tema t = new Tema(Integer.parseInt(request.getParameter("tema")), (String)session.getAttribute("usuarioIdentificado"), request.getParameter("asunto"));
                        Persistencia.insertTema(t);

                        //Vamos a la página de creación de Mensajes.
                        String url = "/nuevoMensaje.jsp?tema=" + Persistencia.obtieneMaxIdTemas();
                        ServletContext sc = getServletContext();
                        RequestDispatcher rd = sc.getRequestDispatcher(url);
                        rd.forward(request, response);
                    }
                    else{
                        session.setAttribute("errorTema", "El Asunto está vacio");
                    }
                    
                    String url = "/nuevoTema.jsp?tema=" + request.getParameter("tema");
                    ServletContext sc = getServletContext();
                    RequestDispatcher rd = sc.getRequestDispatcher(url);
                    rd.forward(request, response);

                } catch (SQLException ex) {
                    Logger.getLogger(ServletNuevoTema.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ServletNuevoTema.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ServletNuevoTema.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
